﻿import com.crystalcentral.*;
import com.crystalcentral.model.*;
import com.crystalcentral.view.*;
import com.crystalcentral.util.*;

import mx.utils.Delegate;
import mx.controls.listclasses.DataProvider;
import mx.controls.ComboBox;

dynamic class com.crystalcentral.FontLoader
{
	private static var fontLoader:FontLoader;
	
	var fontListXML:XML;
	var fontItems:Array;
	var dpFonts:Array;
	var diyTool:DiyTool;
	var onLoadCallback:Function;

	private function FontLoader()
	{
		trace("FontLoader()");
		this.diyTool = DiyTool.getInstance();

		fontListXML = new XML();
		fontListXML.onLoad = Delegate.create(this, onLoadFontList);

		fontItems = new Array();
		dpFonts = new Array();
	}
	
	static function getInstance() {
		if (fontLoader == null || fontLoader == undefined) {
			fontLoader = new FontLoader();
		}
		return fontLoader;
	}

	function loadFontList(file:String, callback:Function)
	{
		trace("FontLoader:loadFontList(" + file + ")");
		this.onLoadCallback = callback;
		fontListXML.ignoreWhite = true;
		fontListXML.load(file);
	}

	function onLoadFontList(success:Boolean):Void
	{
		if (success)
		{
			for (var i = 0; i < fontListXML.firstChild.childNodes.length; i++)
			{

				if (fontListXML.firstChild.childNodes[i].nodeName == "seperator")
				{
					dpFonts.addItem({label:"", data:"_seperator", icon:"gSeperator", fname:"----------------------------------"});
				}
				else
				{
					var lab = fontListXML.firstChild.childNodes[i].firstChild.nodeValue;
					var attr = fontListXML.firstChild.childNodes[i].attributes;
					var fontItem = new FontItem();
					fontItem.fontLabel = lab;
					fontItem.isRotate = (attr["frotate"] == "yes");
					fontItem.fontName = attr["fontname"];
					fontItem.fNormal = attr["fnormal"];
					fontItem.fBold = attr["fbold"];
					fontItem.fItalic = attr["fitalic"];
					fontItem.fBoldItalic = attr["fbolditalic"];
					fontItems.push(fontItem);
					//trace(fontItem);

					var dat = fontItem.fontName;
					//dpFonts.addItem({label:lab, data:dat});
					dpFonts.addItem({label:"", data:dat, icon:"g" + lab, fname:lab});
					//trace("load font: "+lab);

				}
			}

			//trace(fontItems);

			onLoadCallback.apply(diyTool);
		}
		else
		{
			trace("cannot load font list xml");
		}
		diyTool.stopLoading();
	}

	function getFontItemByLabel(label:String):FontItem
	{
		for (var i = 0; i < fontItems.length; i++)
		{
			if (fontItems[i].fontLabel == label)
			{
				return fontItems[i];
			}
		}
		return null;
	}

	function getFontFile(fontItem:FontItem, diyItem:DiyTextItem)
	{
		var fontFile:String;
		if (!diyItem.bold && !diyItem.italic)
		{
			fontFile = fontItem.fNormal;
		}
		else if (diyItem.bold && !diyItem.italic)
		{
			fontFile = fontItem.fBold;
		}
		else if (!diyItem.bold && diyItem.italic)
		{
			fontFile = fontItem.fItalic;
		}
		else
		{
			fontFile = fontItem.fBoldItalic;
		}
		
		/*if (fontFile == undefined || fontFile == null)
		{
			fontFile = fontItem.fNormal;
		}*/

		return fontFile;
	}
	
	// init CheckBox cbFontList
	function initCbFontList(cbFontList:ComboBox) {
		cbFontList.dataProvider = dpFonts;
		cbFontList.dropdown.iconField = "icon";
		cbFontList.selectedIndex = 11;
		_root.textTools.txtFontName.text = cbFontList.selectedItem.fname;
	}
}